import api from '../assets/js/api.js'
import List from '../assets/js/list.js'
function baseModel() {
	return {
		listOption:{
			_where:'',
			_order:'',
			_search:'',
			_g:'',
			_ig:'',
			_option:['none'],
			page:1,
			limit:10,
		},
		list:new List(),//列表class
		//获取
		getOne(id,cbFunc = ()=>{}) {
			let _this = this
			let p = new Promise((resolve,reject)=>{
				api.get("/ez/api/" + _this.name_ + "/" + id, _this.listOption, res => {
					if(res.s*1 === 1){
						this.entity = res.d
						resolve(res.d)
						cbFunc(res.d)
					}
				})
			})
			return p
		},
		get(id,cbFunc = ()=>{}) {
			this.getOne(id).then((data)=>{
				this.entity = data
				cbFunc(data)
			})
			return this
		},
		/*
		 * @param {Object} _this
		 * @param {Object} kName
		 * @param {Object} id
		 */
		one(_this,kName,id,cbFunc = ()=>{}){
			this.getOne(id).then((data)=>{
				_this[kName] = data
				this.entity = data
				cbFunc(data)
			})
			return this
		},
		setOption(option){
			if(option._where != undefined){
				this.listOption._where = option._where
			}
			if(option._order != undefined){
				this.listOption._order = option._order
			}
			if(option._search != undefined){
				this.listOption._search = option._search
			}
			if(option.page != undefined){
				this.listOption.page = option.page
			}
			if(option.limit != undefined){
				this.listOption.limit = option.limit
			}
			if(option.group != undefined){
				this.listOption._g = option.group
			}
			if(option.ignore != undefined){
				this.listOption._ig = option.ignore
			}
			if(option.option != undefined){
				this.listOption._option = option.option
			}
			this.list.setData(this.listOption)
			return this
		},
		setGroup(group){
			if(group){
				this.listOption._g = group
				this.list.setData(this.listOption)
			}
			return this
		},
		setIgnore(ignore){
			if(ignore){
				this.listOption._ig = ignore
				this.list.setData(this.listOption)
			}
			return this
		},
		setLimit(limit){
			if(limit){
				this.listOption.limit = limit
				this.list.setData(this.listOption)
			}
			return this
		},
		setPage(page){
			if(page != undefined){
				this.listOption.page = page
				this.list.setPage(page)
			}
			return this
		},
		setSearch(_search){
			if(_search != undefined){
				this.listOption._search = _search
				this.list.setData(this.listOption)
			}
			return this
		},
		setOrder(_order){
			if(_order != undefined){
				this.listOption._order = _order
				this.list.setData(this.listOption)
			}
			return this
		},
		setWhere(_where){
			if(_where != undefined){
				this.listOption._where = _where
				this.list.setData(this.listOption)
			}
			return this
		},
		setMy(){
			this.listOption._option.push('my')
			return this
		},
		getMany(cbFunc = ()=>{}) {
			let _this = this
			let p = new Promise((resolve,reject)=>{
				api.get("/ez/api/" + _this.name_ , _this.listOption, res => {
					if(res.s*1 === 1){
						resolve(res.d)
						cbFunc(res.d)
					}
				})
			})
			return p
		},
		many(_this,kName){
			this.getMany().then((data)=>{
				_this[kName] = data
			})
			return this
		},
		init(_vue){
			let _this =this
			this.list = new List()
			this.list.initMescroll(_vue).setUrl('/ez/api/' + this.name_)
			return this
		},
		setCbFunc(func){
			this.list.setCbFunc(res => {
				func(res)
			})
			return this
		},
		getList(){
			this.list.setData(this.listOption)
			this.list.getList()
			return this
		},
		reload(){
			this.list.reload()
			return this
		},
		
		//删改查
		setModel(model){
			this.model = model
			return this
		},
		create(cbFunc){
			api.post('/ez/api/' + this.name_,this.model,res=>{
				console.log(res)
				cbFunc(res)
			})
		},
		update(cbFunc){
			api.put('/ez/api/' + this.name_,this.model,res=>{
				console.log(res)
				cbFunc(res)
				
			})
		},
		delete(id,cbFunc=()=>{}){
			api.delete('/ez/api/' + this.name_ + "/" + id,{},res=>{
				console.log(res)
				cbFunc(res)
			})
		},
	}
}
module.exports = baseModel
